package com.yhh.college_information_platform.controller;

import com.yhh.college_information_platform.annotation.RequireAuth;
import com.yhh.college_information_platform.common.PageResult;
import com.yhh.college_information_platform.common.Result;
import com.yhh.college_information_platform.service.FavoriteService;
import com.yhh.college_information_platform.vo.ResourceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 收藏管理Controller（所有接口需要登录）
 *
 * @author yhh
 * @date 2025-10-25
 */
@Slf4j
@RestController
@RequestMapping("/favorite")
@RequireAuth  // 整个Controller都需要登录
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 收藏资料
     */
    @PostMapping("/{resourceId}")
    public Result<Void> addFavorite(@PathVariable Long resourceId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            try {
                com.yhh.college_information_platform.utils.JwtUtil jwtUtil =
                        org.springframework.web.context.support.WebApplicationContextUtils
                                .getRequiredWebApplicationContext(request.getServletContext())
                                .getBean(com.yhh.college_information_platform.utils.JwtUtil.class);
                if (token != null && jwtUtil.validateToken(token)) {
                    userId = jwtUtil.getUserIdFromToken(token);
                    request.setAttribute("userId", userId);
                }
            } catch (Exception ignore) {}
        }
        favoriteService.addFavorite(userId, resourceId);
        return Result.success();
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/{resourceId}")
    public Result<Void> removeFavorite(@PathVariable Long resourceId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        favoriteService.removeFavorite(userId, resourceId);
        return Result.success();
    }

    /**
     * 判断是否收藏
     */
    @GetMapping("/check/{resourceId}")
    public Result<Boolean> isFavorited(@PathVariable Long resourceId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean isFavorited = favoriteService.isFavorited(userId, resourceId);
        return Result.success(isFavorited);
    }

    /**
     * 获取用户的收藏列表
     */
    @GetMapping("/my")
    public Result<PageResult<ResourceVO>> getUserFavorites(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        PageResult<ResourceVO> pageResult = favoriteService.getUserFavorites(userId, current, size);
        return Result.success(pageResult);
    }
}

